package com.ruoyi.web.controller;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.ResultFileDTO;
import com.ruoyi.system.domain.vo.FileVO;
import com.ruoyi.system.service.IUploadFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.alibaba.excel.EasyExcel;
import org.springframework.stereotype.Service;


import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * @author Ly lilongfei
 * @date 2024/8/23
 * @Description
 */
@RestController
public class UploadFileController {

    @Autowired
    private IUploadFileService uploadFileService;

    @PostMapping("/importExcelFile")
    public AjaxResult uploadFile(ResultFileDTO resultFileDTO) {
        //文件
        MultipartFile multipartFile = resultFileDTO.getExcelFile();

        File file = uploadFileService.multipartFile2File(multipartFile);
        List<FileVO> list = uploadFileService.readExcel(file);
        uploadFileService.saveDatatoDataBase(list,resultFileDTO);
        return AjaxResult.success(resultFileDTO.getAthleteName()+"成绩文件上传成功");
    }

    /**
     * 成绩PDF上传存档
     * @param file
     * @return
     */
    @PostMapping("/uploadFile")
    public AjaxResult uploadPDF(MultipartFile file) {
        if(file.isEmpty()){
            return AjaxResult.error("文件为空");
        }
        try {
            byte[] bytes = file.getBytes();
            String uploadDir = "/home/hbshooting/Game_Result_PDF";
//            String testDir = "F:\\Apifox";
            File uploadedFile = new File(uploadDir +"/" + file.getOriginalFilename());
            file.transferTo(uploadedFile);
            return AjaxResult.success(file.getOriginalFilename()+"文件上传成功");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }




}
